package com.chenyue.cm.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenyue.cm.account.domain.JdAccountRecord;
import com.chenyue.cm.account.domain.JdAccountRecordExample;
import com.chenyue.cm.account.service.JdAccountRecordService;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author libingxing
 * @date 2021-07-07
 */
@RestController
@RequestMapping("/jdAccountRecord")
public class JdAccountRecordController {

    private final JdAccountRecordService jdAccountRecordService;

    public JdAccountRecordController(JdAccountRecordService jdAccountRecordService) {
        this.jdAccountRecordService = jdAccountRecordService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdAccountRecord> result = jdAccountRecordService.getPageBean(pageInfo, JdAccountRecord.class);
        return Success.success(result);
    }

    @RequestMapping("/getUserExchangeList")
    public Success getUserExchangeList(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String type = request.getParameter("type");
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        QueryWrapper<JdAccountRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        if (!"0".equals(type)) {
            wrapper.eq("change_type", type);
        }
        List<JdAccountRecord> list = jdAccountRecordService.list(wrapper);
        //封装返回结果
        PageInfo<JdAccountRecord> result = new PageInfo<JdAccountRecord>(list);

        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdAccountRecord vo) {
        boolean success = jdAccountRecordService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdAccountRecord vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdAccountRecordService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdAccountRecordExample aExample = new JdAccountRecordExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdAccountRecordService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdAccountRecordService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

}
